Code Appendix
Cookbook.py
import pygame
import sys
import time
import os
# Command list
COMMANDS = ['start', 'back', 'steps', 'video', 'timer', 'reset', 'spaghetti', 'fish', 'chicken', 'soup', 'sushi', 'pause', 'stop', 'menu']
pygame.init()
# Size
screen_w=800
screen_h=480
# Colors
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
blue=(0,0,255)
# Environment
screen=pygame.display.set_mode([screen_w,screen_h])
icon = pygame.image.load('./images/icon.jpg')
pygame.display.set_icon(icon)
pygame.display.set_caption('Cookbook')
clock=pygame.time.Clock()
arial_25 = pygame.font.SysFont('arial',25)
clock_font = pygame.font.SysFont('DejaVu Sans Mono',26)
#Loading Sequence
op1 = pygame.image.load('./images/Loading.png').convert_alpha()
op1 = pygame.transform.scale(op1,(screen_w,screen_h))
op2 = pygame.image.load('./images/Home.png').convert_alpha()
op2 = pygame.transform.scale(op2,(screen_w,screen_h))
op3 = pygame.image.load('./images/start.png').convert_alpha()
op3 = pygame.transform.scale(op3,(screen_w,screen_h))
menu_level = None
# Check voice command
def check(cmd):
if cmd in COMMANDS:
return cmd
return None
# Voice detection
def voice_detect():
f = open("test.txt", mode='r+', encoding='utf-8')
command = f.read()
found = check(command)
if found is not None:
print("[main] Command found: " + found)
f.truncate(0)
f.close()
return found
else:
f.close()
return None
# Button get pressed
def button(msg,x,y,w,h,ap,ic,ac):
valid = False
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w>mouse[0]>x and y+h>mouse[1]>y:
pygame.draw.rect(screen,ac,(x-ap,y-ap,w+2*ap,h+2*ap))
if click[0] == 1:
valid = True
time.sleep(0.1)
else:
pygame.draw.rect(screen,ic,(x,y,w,h))
txt = arial_25.render(msg,True,black)
txt_rect = txt.get_rect()
txt_rect.center = ((x+w/2),(y+h/2))
screen.blit(txt,txt_rect)
return valid
# ----- Spaghetti -----
def Spaghetti():
intro = True
spaghettiimg1 = pygame.image.load('./images/spaghetti1.jpg').convert()# spaghetti picture
spaghettiimg2 = pygame.image.load('./images/spaghetti2.jpg').convert()# ingredients list
global menu_level
start = pygame.time.get_ticks()
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
# Show ingredients
if pygame.time.get_ticks()-start<2000:# For 2 seconds, display the spaghetti picture
screen.blit(spaghettiimg1,(0,0))
else: #After 2 seconds have passed, display the ingredients list
screen.blit(spaghettiimg2,(0,0))
screen.blit(timeText, (670,0))
# Check voice command from external txt file
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
if button('Show Steps',340,440,130,35,3,(242,242,242),(255,255,153)) or voice == "steps":
intro = False
menu_level = Spaghetti_steps# Move to steps
if button('Menu',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "menu":
intro = False
menu_level = menu# Move to menu
###
pygame.display.update()
clock.tick(60)
# Each of the recipes that follow are formatted the same way. All that changes are pictures and videos.
def Spaghetti_steps():
intro = True
spaghettiimg = pygame.image.load('./images/spaghetti3.jpg').convert()
global menu_level
timer = pygame.time.Clock()
timer_running = False
frame_count = 0
frame_rate = 60
start_time = 90
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current local time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
screen.blit(spaghettiimg,(0,0))
screen.blit(timeText, (670,0))
# Check voice command from external txt file
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
# Display Timer Buttons
if(timer_running):
if button('Reset Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "reset": # Timer
timer_running = False
else:
if button('Start Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "timer": # Timer
timer_running = True
# Timer countsdown
if(timer_running):
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 0:
total_seconds = 0
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
mytimer = clock_font.render(output_string, True, (0,0,0))
frame_count += 1
screen.blit(mytimer, (250,0))
timer.tick(frame_rate)
else:
frame_count = 0
start_time = 90
# Video button
if button('Video',670,90,130,35,3,(242,242,242),(255,255,153)) or voice == "video": # Video
os.system('mplayer -fs -x 800 -y 480 ./videos/spaghetti.mp4')
# Back button
if button('Back',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "back": # Back
intro = False
menu_level = Spaghetti
pygame.display.update()
clock.tick(60)
# ----- Fish -----
def Fish():
intro = True
fishimg1 = pygame.image.load('./images/fish1.jpg').convert()
fishimg2 = pygame.image.load('./images/fish2.jpg').convert()
global menu_level
start = pygame.time.get_ticks()
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
if pygame.time.get_ticks()-start<2000:
screen.blit(fishimg1,(0,0))
else:
screen.blit(fishimg2,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
if button('Show Steps',340,440,130,35,3,(242,242,242),(255,255,153)) or voice == "steps":
intro = False
menu_level = Fish_steps
if button('Menu',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "menu":
intro = False
menu_level = menu
###
pygame.display.update()
clock.tick(60)
def Fish_steps():
intro = True
spaghettiimg = pygame.image.load('./images/fish3.jpg').convert()
global menu_level
timer = pygame.time.Clock()
timer_running = False
frame_count = 0
frame_rate = 60
start_time = 90
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current local time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
screen.blit(spaghettiimg,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
# Display Timer Buttons
if(timer_running):
if button('Reset Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "reset": # Timer
timer_running = False
else:
if button('Start Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "timer": # Timer
timer_running = True
# Timer countsdown
if(timer_running):
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 0:
total_seconds = 0
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
mytimer = clock_font.render(output_string, True, (0,0,0))
frame_count += 1
screen.blit(mytimer, (250,0))
timer.tick(frame_rate)
else:
frame_count = 0
frame_rate = 60
start_time = 90
# Video button
if button('Video',670,90,130,35,3,(242,242,242),(255,255,153)) or voice == "video": # Video
os.system('mplayer -fs -x 800 -y 480 ./videos/fish.mp4')
# Back button
if button('Back',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "back": # Back
intro = False
menu_level = Fish
pygame.display.update()
clock.tick(60)
# ----- Chicken -----
def Chicken():
intro = True
chickenimg1 = pygame.image.load('./images/chicken1.jpg').convert()
chickenimg2 = pygame.image.load('./images/chicken2.jpg').convert()
global menu_level
start = pygame.time.get_ticks()
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
if pygame.time.get_ticks()-start<2000:
screen.blit(chickenimg1,(0,0))
else:
screen.blit(chickenimg2,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
if button('Show Steps',340,440,130,35,3,(242,242,242),(255,255,153)) or voice == "steps":
intro = False
menu_level = Chicken_steps
if button('Menu',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "menu":
intro = False
menu_level = menu
###
pygame.display.update()
clock.tick(60)
def Chicken_steps():
intro = True
spaghettiimg = pygame.image.load('./images/chicken3.jpg').convert()
global menu_level
timer = pygame.time.Clock()
timer_running = False
frame_count = 0
frame_rate = 60
start_time = 90
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current local time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
screen.blit(spaghettiimg,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
# Display Timer Buttons
if(timer_running):
if button('Reset Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "reset": # Timer
timer_running = False
else:
if button('Start Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "timer": # Timer
timer_running = True
# Timer countsdown
if(timer_running):
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 0:
total_seconds = 0
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
mytimer = clock_font.render(output_string, True, (0,0,0))
frame_count += 1
screen.blit(mytimer, (250,0))
timer.tick(frame_rate)
else:
frame_count = 0
frame_rate = 60
start_time = 90
# Video button
if button('Video',670,90,130,35,3,(242,242,242),(255,255,153)) or voice == "video": # Video
os.system('mplayer -fs -x 800 -y 480 ./videos/chicken.mp4')
# Back button
if button('Back',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "back": # Back
intro = False
menu_level = Chicken
pygame.display.update()
clock.tick(60)
# ----- Soup -----
def Soup():
intro = True
soupimg1 = pygame.image.load('./images/soup1.jpg').convert()
soupimg2 = pygame.image.load('./images/soup2.jpg').convert()
global menu_level
start = pygame.time.get_ticks()
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
if pygame.time.get_ticks()-start<2000:
screen.blit(soupimg1,(0,0))
else:
screen.blit(soupimg2,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
if button('Show Steps',340,440,130,35,3,(242,242,242),(255,255,153)) or voice == "steps":
intro = False
menu_level = Soup_steps
if button('Menu',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "menu":
intro = False
menu_level = menu
###
pygame.display.update()
clock.tick(60)
def Soup_steps():
intro = True
spaghettiimg = pygame.image.load('./images/soup3.jpg').convert()
global menu_level
timer = pygame.time.Clock()
timer_running = False
frame_count = 0
frame_rate = 60
start_time = 90
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current local time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
screen.blit(spaghettiimg,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
# Display Timer Buttons
if(timer_running):
if button('Reset Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "reset": # Timer
timer_running = False
else:
if button('Start Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "timer": # Timer
timer_running = True
# Timer countsdown
if(timer_running):
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 0:
total_seconds = 0
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
mytimer = clock_font.render(output_string, True, (0,0,0))
frame_count += 1
screen.blit(mytimer, (250,0))
timer.tick(frame_rate)
else:
frame_count = 0
frame_rate = 60
start_time = 90
# Video button
if button('Video',670,90,130,35,3,(242,242,242),(255,255,153)) or voice == "video": # Video
os.system('mplayer -fs -x 800 -y 480 ./videos/soup.mp4')
# Back button
if button('Back',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "back": # Back
intro = False
menu_level = Soup
pygame.display.update()
clock.tick(60)
# ----- Sushi -----
def Sushi():
intro = True
sushiimg1 = pygame.image.load('./images/sushi1.jpg').convert()
sushiimg2 = pygame.image.load('./images/sushi2.jpg').convert()
global menu_level
start = pygame.time.get_ticks()
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
if pygame.time.get_ticks()-start<2000:
screen.blit(sushiimg1,(0,0))
else:
screen.blit(sushiimg2,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
if button('Show Steps',340,440,130,35,3,(242,242,242),(255,255,153)) or voice == "steps":
intro = False
menu_level = Sushi_steps
if button('Menu',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "menu":
intro = False
menu_level = menu
###
pygame.display.update()
clock.tick(60)
def Sushi_steps():
intro = True
spaghettiimg = pygame.image.load('./images/sushi3.jpg').convert()
global menu_level
timer = pygame.time.Clock()
timer_running = False
frame_count = 0
frame_rate = 60
start_time = 90
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current local time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
screen.blit(spaghettiimg,(0,0))
screen.blit(timeText, (670,0))
voice = voice_detect()
# buttons (msg,x,y,w,h,ap, color-before-hover, color-during-hover,action)
# Display Timer Buttons
if(timer_running):
if button('Reset Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "reset": # Timer
timer_running = False
else:
if button('Start Timer',670,40,130,35,3,(242,242,242),(255,255,153)) or voice == "timer": # Timer
timer_running = True
# Timer countsdown
if(timer_running):
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 0:
total_seconds = 0
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
mytimer = clock_font.render(output_string, True, (0,0,0))
frame_count += 1
screen.blit(mytimer, (250,0))
timer.tick(frame_rate)
else:
frame_count = 0
frame_rate = 60
start_time = 90
# Video button
if button('Video',670,90,130,35,3,(242,242,242),(255,255,153)) or voice == "video": # Video
os.system('mplayer -fs -x 800 -y 480 ./videos/sushi.mp4')
# Back button
if button('Back',0,0,100,40,3,(242,242,242),(255,255,153)) or voice == "back": # Back
intro = False
menu_level = Sushi
pygame.display.update()
clock.tick(60)
def menu():
intro = True
menuimg = pygame.image.load('./images/menu.jpg').convert()
global menu_level
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
# Display current local time
theTime=time.strftime("%H:%M:%S", time.localtime())
timeText=clock_font.render(str(theTime), True,(0,0,0))
screen.blit(menuimg,(0,0))
screen.blit(timeText, (670,0))
# Check voice command from external txt file
voice = voice_detect()
# Food buttons
if button('Spaghetti',45,430,100,40,3,(242,242,242),(255,255,153)) or voice == "spaghetti":
intro = False
menu_level = Spaghetti
if button('Fish',197,430,100,40,3,(242,242,242),(255,255,153)) or voice == "fish":
intro = False
menu_level = Fish
if button('Chicken',348,430,100,40,3,(242,242,242),(255,255,153)) or voice == "chicken":
intro = False
menu_level = Chicken
if button('Soup',500,430,100,40,3,(242,242,242),(255,255,153)) or voice == "soup":
intro = False
menu_level = Soup
if button('Sushi',652,430,100,40,3,(242,242,242),(255,255,153)) or voice == "sushi":
intro = False
menu_level = Sushi
if button('X',760,40,40,40,3,(242,242,242),(255,255,153)) or voice == "quit":
intro = False
menu_level = quit_cookbook
pygame.display.update()
clock.tick(60)
def op():
global menu_level
start = pygame.time.get_ticks()
cinematic = True
while cinematic:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE or event.type == pygame.MOUSEBUTTONDOWN:
menu_level = menu
cinematic = False
# First splash screen (start time < 2secs)
if pygame.time.get_ticks()-start<2000:
screen.blit(op1,(0,0))
# Second splash screen (2secs <= start time < 4secs)
elif 2000<pygame.time.get_ticks()-start<4000:
screen.blit(op2,(0,0))
# Third splash screen with start button
else:
screen.blit(op3,(0,0))
# Check voice command is "start" or no
voice = voice_detect()
# Touch detection
if event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN or voice == "start":
menu_level = menu
cinematic = False
pygame.display.update()
clock.tick(60)
# Quit
def quit_cookbook():
pygame.quit()
sys.exit()
# Main Entrance func.
def run():
global menu_level
menu_level = op
while True:
menu_level()
if __name__ == "__main__":
run()
Voice_and_Motion_Control.py
import time
import speech_recognition as sr
import RPi.GPIO as GPIO
from record import RecordAudio
import speech
from gpiozero import Servo
import keyboard
freq = 50
direction = False #left
move = False
position = "mid"
inrange= False
running = True
back = False
# Support commands dict
COMMANDS = {
'start': ['START', 'STARTS'], # Multiple command options account for audio processing mistakes
'back': ['BACK'],
'steps': ['STEP', 'STEPS','NEXT'],
'video': ['VIDEO', 'VIDEOS'],
'spaghetti': ['SPAGHETTI'],
'fish': ['FISH'],
'chicken': ['CHICKEN'],
'soup': ['SOUP'],
'sushi': ['SUSHI'],
'menu': ['MENU'],
'timer': ['TIMER'],
'reset': ['RESET'],
'pause': ['PAUSE', 'CONTINUE'],
'stop': ['STOP'],
'quit': ['QUIT'],
'motion': ['MOTION', 'MOTIONS', 'NOTION']
}
# Hardware interrupt (Active low interrupt. Triggered when button is pressed connecting the pin to ground.)
def GPIO16_callback(channel):
global back
print ("falling edge detected on 16")
back = True
# Record an audio and send to Google
def recognize(rec, mic):
running = True
result = ""
print("[main] Recognizing...")
GPIO.output(21,GPIO.HIGH)
mic.record()
GPIO.output(21,GPIO.LOW)
result = speech.recognize(mic.save())
time.sleep(1)
return result
# Check if the speech command is in the command dict.
def check(cmd):
for key, val in COMMANDS.items():
if cmd in val:
return key
return None
def run():
global freq
global direction
global move
global position
global running
global back
servo = Servo(13)
val=0
servo.value = val
# Initialize speech recognition
rec = sr.Recognizer()
mic = RecordAudio()
motion = False
start_time = 0
while running:
if back == True: # Execute shutdown protocol
print("button pressed")
running = False # Before ending script, return servo to initial position (val = 0)
while val < 0:
print(val)
val = val + .05
servo.value = val
time.sleep(.1)
while val >0:
print(val)
val = val - .05
servo.value = val
time.sleep(0.1)
break
# Voice detection part
if motion == False:
# Introduction
print("[main] Please speak a command")
command = None
while command is None:
if back == True:
break
command = recognize(rec, mic)
if back == True:
continue
print("[Result] " + command)
found = check(command.upper())
if found is not None:
print("[main] Command found: " + found)
f = open("test.txt", mode='w', encoding='utf-8')
f.write(found)
f.close()
#fifo_cmd = 'echo ' + command + ' > test_fifo'
#subprocess.check_output(fifo_cmd, shell=True)
# Start motion control
if command.upper() == 'MOTION':
start_time = time.time()
motion = True
# Stop execution if EXIT
if command.upper() == 'QUIT':
running = False
# Motion control part
else: # User has said 'MOTION'
if time.time() - start_time > 10:# 10 seconds for motion control operation
motion = False
GPIO.output(4,GPIO.LOW)# Trigger (Left sensor)
GPIO.output(23,GPIO.LOW)# Trigger (Right sensor)
#print ("Waiting for sensors to settle")
time.sleep(.01)
#print ("Calculating distances")
GPIO.output(4,GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(4,GPIO.LOW)
while GPIO.input(17)==0: # Echo (Left sensor)
pulse_start_time = time.time()
#print('bit flip')
while GPIO.input(17)==1: # Echo recieved (left sensor)
pulse_end_time=time.time()
pulse_duration = pulse_end_time - pulse_start_time
Ldistance = round(pulse_duration *17150, 2) # compute left distance
#print ("Left Distance:",Ldistance,"cm")
GPIO.output(23,GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(23,GPIO.LOW)
while GPIO.input(24)==0:
pulse_start_time = time.time()
#print('bit flip')
while GPIO.input(24)==1:
pulse_end_time=time.time()
pulse_duration = pulse_end_time - pulse_start_time
Rdistance = round(pulse_duration *17150, 2) # compute right distance
#print (" Right Distance:",Rdistance,"cm")
if Ldistance < 8:# decrement motor if left sensor activated
if val > -0.95:# Ensure servo val does not exceed limit
servo.value = val
val = val-0.05
print(val)
time.sleep(.1)
if Rdistance < 8:# increment if right sensor activated
if val < 0.95:# Ensure servo val does not exceed limit
servo.value = val
val = val+0.05
print(val)
time.sleep(.1)
if keyboard.is_pressed("space"):# Execute shutdown protocol if spacebar is pressed
running = False
while val < 0:
print(val)
val = val + .05
servo.value = val
time.sleep(.1)
while val >0:
print(val)
val = val - .05
servo.value = val
time.sleep(0.1)
if __name__ == "__main__":
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(13, GPIO.OUT)
GPIO.setup(4, GPIO.OUT)
GPIO.setup(17, GPIO.IN)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.IN)
GPIO.setup(16, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(16, GPIO.FALLING, callback=GPIO16_callback, bouncetime=300)
try:
run()
except KeyboardInterrupt:
GPIO.cleanup()